package com.fsnbigdata.repository;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.OrganismVideo;
import com.fsnbigdata.domain.Organism;
import com.fsnbigdata.domain.QOrganismVideo;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * = OrganismVideoRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = OrganismVideoRepositoryCustom.class)
@Transactional(readOnly = true)
public class OrganismVideoRepositoryImpl extends QueryDslRepositorySupportExt<OrganismVideo> implements OrganismVideoRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    OrganismVideoRepositoryImpl() {
        super(OrganismVideo.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ORGANISM = "organism";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String URL = "url";

    /**
     * TODO Auto-generated method documentation
     *
     * @param organism
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<OrganismVideo> findByOrganism(Organism organism, GlobalSearch globalSearch, Pageable pageable) {
        QOrganismVideo organismVideo = QOrganismVideo.organismVideo;
        JPQLQuery<OrganismVideo> query = from(organismVideo);
        Assert.notNull(organism, "organism is required");
        query.where(organismVideo.organism.eq(organism));
        Path<?>[] paths = new Path<?>[] { organismVideo.organism, organismVideo.url };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(ORGANISM, organismVideo.organism).map(URL, organismVideo.url);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, organismVideo);
    }
}
